PetBattleQuality = {}
local breedInfo = LibStub("LibPetBreedInfo-1.0")

local function GetColor(confidence)
	local color = "|cffffcc00"
	if confidence < 2.5 then
		color = "|cff888888"
	end
	return color
end
local breedFormat = "%s %s(%s)|r"
local qualityFormat = "|c%s%s|r"

local function CreateGlow()
	local frame = CreateFrame("Frame", nil)
	frame.texture = frame:CreateTexture(nil, "ARTWORK")
	frame.texture:SetTexture("Interface\\Buttons\\UI-ActionButton-Border", false)
	frame.texture:SetBlendMode("ADD")
	frame.texture:SetDrawLayer("ARTWORK", 1)
	frame:Hide()
	return frame
end

local function Hook_PetBattleUnitFrame_UpdateDisplay(self)
	if not self.petOwner or not self.petIndex then return end
	
	local frame = PetBattleQuality.frames[self.petOwner][self.petIndex]
	if self:GetName() == "PetBattlePrimaryUnitTooltip" then frame = PetBattleQuality.tooltip end
	local rarity = C_PetBattles.GetBreedQuality(self.petOwner, self.petIndex) 
	local r, g, b, hex = GetItemQualityColor(rarity - 1)
	
	frame:SetParent(self)
	frame:ClearAllPoints()
	frame:SetWidth(self.Icon:GetWidth() * 1.7)
	frame:SetHeight(self.Icon:GetHeight() * 1.7)
	frame:SetPoint("CENTER", self.Icon, "CENTER", 0, 0)
	
	frame.texture:SetParent(self)
	frame.texture:ClearAllPoints()
	frame.texture:SetAllPoints(frame)
	frame.texture:SetVertexColor(r, g, b)
	frame.texture:SetShown(false)
	
	if  frame ~= PetBattleQuality.tooltip then
		if (self.ActualHealthBar) then
			if (self.petIndex ~= C_PetBattles.GetActivePet(self.petOwner)) then
				self.ActualHealthBar:SetVertexColor(r, g, b)
			else
				self.ActualHealthBar:SetVertexColor(0, 1,0)
			end
		end
		
		if self.BorderAlive then
			self.BorderAlive:SetVertexColor(r, g, b)
		end
	end
	
	if PetBattleInfo.db.global.display.breed then
		local breedIndex, Confidence = breedInfo:GetBreedByPetBattleSlot(self.petOwner, self.petIndex)
		if breedIndex then
			local breedName = breedInfo:GetBreedName(breedIndex)
			
			if self.Name then
				self.Name:SetText(string.format(breedFormat,self.Name:GetText(),GetColor(Confidence),breedName))
			end	
		end
		
	end
	
end



PetBattleQuality.tooltip = CreateGlow()
PetBattleQuality.frames = {}
for i=1,2 do
	PetBattleQuality.frames[i] = {}
	for j=1,3 do 
		PetBattleQuality.frames[i][j] = CreateGlow()
	end
end
hooksecurefunc("PetBattleUnitFrame_UpdateDisplay", Hook_PetBattleUnitFrame_UpdateDisplay)



